﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.BLL;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.shop
{
    public partial class user_bank_edit : MxWeiXinPF.Web.UI.UserPage
    {
        private string action = MXEnums.ActionEnum.Add.ToString();
        private int id = 0;
        private string openid = "";
        private int wid = 0;

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.wid == 0)
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"请从商城个人中心中进入后，再录入银行信息\");</script>");
            }
            else if (this.openid.Equals(""))
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"请在微信中打开页面，再录入银行信息\");</script>");
            }
            else if (this.action == MXEnums.ActionEnum.Edit.ToString())
            {
                if (!this.DoEdit(this.id))
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"保存过程中发生错误\");</script>");
                }
                else
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"修改银行帐号信息成功\");</script>");
                }
            }
            else if (!this.DoAdd())
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"保存过程中发生错误\");</script>");
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"添加银行帐号信息成功!\");</script>");
                this.txtTitle.Text = "";
                this.txtBrank.Text = "";
                this.txtBrankNO.Text = "";
                this.txtPhone.Text = "";
                this.txtAddress.Text = "";
                this.hwid.Value = "";
            }
        }

        private bool DoAdd()
        {
            bool flag = false;
            user_rate _rate = new user_rate();
            if (_rate.Add(this.wid, this.openid, this.txtTitle.Text.Trim(), this.txtBrank.Text.Trim(), this.txtBrankNO.Text.Trim(), this.txtPhone.Text.Trim(), this.txtAddress.Text) > 0)
            {
                flag = true;
            }
            return flag;
        }

        private bool DoEdit(int _id)
        {
            bool flag = false;
            user_rate _rate = new user_rate();
            if (_rate.Update(this.wid, this.openid, this.txtTitle.Text.Trim(), this.txtBrank.Text.Trim(), this.txtBrankNO.Text.Trim(), this.txtPhone.Text.Trim(), this.txtAddress.Text, _id))
            {
                flag = true;
            }
            return flag;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string queryString = MXRequest.GetQueryString("action");
                this.wid = MyCommFun.RequestInt("wid");
                this.openid = MyCommFun.RequestOpenid();
                this.shopid = MyCommFun.RequestInt("shopid");
                if (!string.IsNullOrEmpty(queryString) && (queryString == MXEnums.ActionEnum.Edit.ToString()))
                {
                    this.action = MXEnums.ActionEnum.Edit.ToString();
                    this.id = MXRequest.GetQueryInt("id");
                    if (this.id == 0)
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"传输参数不正确\");</script>");
                        return;
                    }
                }
                if (!this.Page.IsPostBack && (this.action == MXEnums.ActionEnum.Edit.ToString()))
                {
                    this.ShowInfo(this.id);
                }
            }
            catch (Exception ex)
            {

            }

        }

        private void ShowInfo(int _id)
        {
            DataSet bankByID = new user_rate().GetBankByID(_id);
            if ((bankByID != null) && (bankByID.Tables[0].Rows.Count > 0))
            {
                this.txtTitle.Text = bankByID.Tables[0].Rows[0]["TrueName"].ToString();
                this.txtBrank.Text = bankByID.Tables[0].Rows[0]["Bank"].ToString();
                this.txtBrankNO.Text = bankByID.Tables[0].Rows[0]["BankNO"].ToString();
                this.txtPhone.Text = bankByID.Tables[0].Rows[0]["Phone"].ToString();
                this.txtAddress.Text = bankByID.Tables[0].Rows[0]["Address"].ToString();
                this.hwid.Value = bankByID.Tables[0].Rows[0]["ID"].ToString();
            }
        }
    }
}